//
//  UIApplication+Extension.m
//  DianBo
//
//  Created by YangBo on 2018/12/11.
//  Copyright © 2018 微光. All rights reserved.
//

#import "UIApplication+Extension.h"
//#import "WGHeader.h"

@implementation UIApplication (Extension)

+ (UIWindow *)getKeyWindow
{
#ifdef DEBUG
    if (UIApplication.sharedApplication.windows.count > 0) {
        UIWindow * keyWindow = UIApplication.sharedApplication.keyWindow;

        if ([keyWindow isKindOfClass:NSClassFromString(@"CocoaDebug.CocoaDebugWindow")]) {
            for (UIWindow * tempWindow in UIApplication.sharedApplication.windows) {
                if (![tempWindow isKindOfClass:NSClassFromString(@"CocoaDebug.CocoaDebugWindow")] && ![tempWindow isKindOfClass:NSClassFromString(@"UITextEffectsWindow")]) {
                    keyWindow = tempWindow;
                    break;
                }
            }
        }
        return keyWindow;
    } else {
        return UIApplication.sharedApplication.keyWindow;
    }

#else
    return UIApplication.sharedApplication.keyWindow;
#endif
}

+ (UIViewController *)wg_rootViewController
{
    return [UIApplication getKeyWindow].rootViewController;
}

+ (UINavigationController *)currentNavigationController
{
    UIViewController * rootVC = [self wg_rootViewController];

    if ([rootVC isKindOfClass:[UITabBarController class]]) {
        UITabBarController * tab = (UITabBarController *)rootVC;
        return (UINavigationController *)tab.selectedViewController;
    }
    return [[UINavigationController alloc] init];
}

@end
